******************************
****					  ****
****     FATA PROJECT     ****
****      (analysis)	  ****
****					  ****
******************************


global PathData = "~/Dropbox/FATA_fractionalization/1_Data/"
global PathRes = "~/Dropbox/FATA_fractionalization/4_Results/"

use "${PathData}fata_data_max_recoded.dta", clear


merge 1:1 rev_resp_id age education mont_incm using "${PathData}fata_otherdiv.dta", force nogenerate

merge m:1 tehs_code using "${PathData}tehs_segr.dta", force nogenerate

merge m:1 vill_code_wrong using "${PathData}pop_landscan.dta", nogenerate force
merge m:1 vill_name agency tehs_name latitude longitude using "${PathData}distances.dta", nogenerate force

recode sex 1=1 0=0, gen(man) 
recode education 0 =1 1/4=2 5/6=3, gen(edu_rec) 
tab edu_rec, gen(educ)
egen victimization = rowtotal(death_sa kidnap_sa fmkidnap_sa ppd_sa fmpd_sa hd_sa ld_sa idp_sa fidp_sa ext_sa fmext_sa) 
egen chronicly_ill = rowtotal(phy_disable per_mentdisb phy_illness)
recode marital_stat 0=1 1/4=0, gen(never_married)
gen lnmont_incm = ln(mont_incm+1)
recode religion 1=0 2=1, gen(shia)
bys vill_code: egen shia_percent = mean(shia)
zval minority_vill minority_tehs Cseg
gen frac_village_minority = z_tribal_frac0*z_minority_vill
gen frac_tehsil_minority = z_Cseg*z_minority_tehs


label variable tribal_frac0 "Fractionalization"
label variable Cseg "Segregation"
label variable z_tribal_frac0 "Fractionalization"
label variable z_Cseg "Segregation"
label variable man "Male"
label variable age "Age"
label variable educ1 "No education"
label variable educ2 "Primary / secondary"
label variable educ3 "Tertiary"
label variable victimization "Victimization index"
label variable chronicly_ill "Illness index"
label variable lnmont_incm "Monthly income (log)"
label variable never_married "Never married"
label variable shia "Shia"
label variable shia_percent "Percent Shia"
label variable minority_vill "Minority (village)"
label variable minority_tehs "Minority (tehsil)"
label variable z_minority_vill "Minority (village)"
label variable z_minority_tehs "Minority (tehsil)"
label variable frac_village_minority "Village frac. x Minority"
label variable frac_tehsil_minority "Tehsil segr. x Minority"

egen services_all = rowmean(rebuilding med_services edu infra water sanitation agri_supp busi_supp bank_ser reli_ser justice security)
egen services = rowmean(rebuilding med_services edu infra water sanitation agri_supp busi_supp bank_ser reli_ser)
egen services_security = rowmean(justice security)
zval services services_security services_all


	replace re_govt=0 if re_govt==. & re_dk!=6
	replace ms_govt=0 if ms_govt==. & ms_dk!=6
	replace edu_govt=0 if edu_govt==. & edu_dk!=6
	replace infra_govt=0 if infra_govt==. & infra_dk!=6
	replace wat_govt=0 if wat_govt==. & wat_dk!=6
	replace sani_govt=0 if sani_govt==. & sani_dk!=6
	replace just_govt=0 if just_govt==.& just_dk!=6
	replace sec_govt=0 if sec_govt==. & sec_dk!=6
	replace as_govt=0 if as_govt==. & as_dk!=6
	replace bs_govt=0 if bs_govt==. & bs_dk!=6
	replace bank_govt=0 if bank_govt==. & bank_dk!=6
	replace rs_govt=0 if rs_govt==. & rs_dk!=6	

	replace re_army=0 if re_army==.& re_dk!=6
	replace ms_army=0 if ms_army==.& ms_dk!=6
	replace edu_army=0 if edu_army==.& edu_dk!=6	
	replace infra_army=0 if infra_army==.& infra_dk!=6	
	replace wat_army=0 if wat_army==.& wat_dk!=6	
	replace sani_army=0 if sani_army==.& sani_dk!=6	
	replace just_army=0 if just_army==.& just_dk!=6	
	replace sec_army=0 if sec_army==.& sec_dk!=6	
	replace as_army=0 if as_army==.& as_dk!=6	
	replace bs_army=0 if bs_army==.& bs_dk!=6	
	replace bank_army=0 if bank_army==. & bank_dk!=6
	replace rs_army=0 if rs_army==. & rs_dk!=6

egen service_govt_temp = rowmean(govt_repr re_govt ms_govt edu_govt infra_govt wat_govt sani_govt as_govt bs_govt bank_govt rs_govt)
gen service_govt = service_govt_temp/2
drop service_govt_temp
egen service_army_temp = rowmean(re_army ms_army edu_army infra_army wat_army sani_army as_army bs_army bank_army rs_army)
gen service_army = service_army_temp/3
drop service_army_temp
zval  service_govt service_army
egen service_state = rowmean(service_govt service_army)
zval service_state

egen security_govt_temp = rowmean(just_govt sec_govt)
gen security_govt = security_govt_temp/2
drop security_govt_temp
egen security_army_temp = rowmean(just_army sec_army)
gen security_army = security_army_temp/3
drop security_army_temp
zval  security_govt security_army
egen security_state = rowmean(security_govt security_army)
zval security_state

egen all_service_state = rowmean(service_govt service_army security_govt security_army)
zval all_service_state



egen ngo_justice = rowmean(ngos_dr)
zval ngo_justice
label variable z_ngo_justice "NGO dispute"

gen re_imp2=re_imp if re_govt==2 
	gen ms_imp2=ms_imp if ms_govt==2 
	gen edu_imp2=edu_imp if edu_govt==2 
	gen infra_imp2=infra_imp if infra_govt==2 
	gen wat_imp2=wat_imp if wat_govt==2 
	replace wat_imp2=1 if wat_imp2==0
	gen sani_imp2=sani_imp if sani_govt==2 
	gen just_imp2=just_imp if just_govt==2 
	gen as_imp2=as_imp if as_govt==2 
	gen bs_imp2=bs_imp if bs_govt==2 
	gen bank_imp2=bank_imp if bank_govt==2 
	gen rs_imp2=rs_imp if rs_govt==2 

egen service_govt_imp_temp = rowmean(re_imp2 ms_imp2 edu_imp2 infra_imp2 wat_imp2 sani_imp2 as_imp2 bs_imp2 bank_imp2 rs_imp2 ) 
gen service_govt_imp = service_govt_imp_temp*(-1)
zval service_govt_imp just_imp2




label variable services  "Local services"    
label variable service_govt_imp "State services importance"
label variable regional_id "Detachment from region"
label variable security "Patrols"
label variable misuse "Fear of exploitation"
label variable trust_other "Trust in other tribes"
label variable ngo_justice "NGO dispute"
label variable taxes1 "State taxation"
label variable enforcement1 "State law enforcement"

drop vill_pop
gen count = 1
bys vill_code: egen vill_pop = sum(count) 
bys vill_code: gen count2=_n 
reg value vill_pop if count2==1 & value!=0
predict pop_imputed
gen pop_imputed2 = 2203.172 + (64.78758 * vill_pop)
gen lnpop = ln(pop_imputed2)


pca taxes1 enforcement1
predict pca1 pca2




egen particip_peace = rowmean(jirga peace_com)
gen informal_justice = 1 if tribal_eld==1 | relig_leader==1 | jirgs==1 | nsa_dr==1 
replace informal_justice = 0  if tribal_eld==0 & relig_leader==0 & jirgs==0 & nsa_dr==0 
recode misuse 6 =3
replace bs_comm=0 if bs_comm==.
zval particip_peace trust_neigh informal_justice  tolerance_rel friends_or attend_meet bs_comm misuse

gen welfare = taxes1-enforcement1
zval welfare




gen categories = . 
replace categories = 1 if tribal_frac0<=.1912965 & Cseg<=.0157278 
replace categories = 2 if tribal_frac0<=.1912965 & Cseg>.0157278 
replace categories = 3 if tribal_frac0>.1912965 & Cseg<=.0157278 
replace categories = 4 if tribal_frac0>.1912965 & Cseg>.0157278 

recode categories (1 = 1 "Homogeneous") (2 = 2 "Segregated") (3 = 3 "Fractionalized") (4 = 4 "Fract. & Segreg."), gen(village_type)



gen x = 1 if vill_pop>=15
bys tehs_code: egen n_vill = sum(x) if count2==1
drop x
bys tehs_code: egen n_vill_mean = mean(n_vill)



gen z_inter = z_tribal_frac0*z_Cseg
label variable z_inter "Frac. x Segr."
label variable pca2 "Welfere vs Security (PCA)"
label variable z_Cseg "Segregation"
label variable z_tribal_frac0 "Fractionalization"


gen education_ihi_01 = education_ihi/100
gen age_ihi_01 = age_ihi/100
gen wealth_ihi_01 = wealth_ihi/100

replace service_state =0 if service_state==.
replace security_state =0 if security_state==. 

label variable education_ihi_01 "Education diversity"
label variable age_ihi_01 "Age diversity"
label variable wealth_ihi_01 "Wealth diversity"
label variable service_state "Welfare services"
label variable security_state "Security services"



******************************
****  DESCRIPTIVE STATS   ****
******************************

gen service_govt_imp2 = service_govt_imp+3
label variable service_govt_imp2 "Govt. serv. importance"


*Tab 1
eststo clear
eststo: estpost sum taxes1 enforcement1 service_govt_imp2 tribal_frac0 Cseg friends_or informal_justice bs_comm trust_neigh misuse particip_peace man age educ1 educ2 educ3 victimization chronicly_ill lnmont_incm never_married ngo_justice  service_state security_state lnpop shia
esttab using ${PathRes}table0_new.rtf, replace cell((mean sd min max)) label mtitles title("Descriptive Statistics") nonotes


*Fig A4 
twoway (scatter tribal_frac0 Cseg) (lfit tribal_frac0 Cseg)




******************************
****     MAIN TABLE       ****
******************************



*Tab 2
eststo clear
eststo Welfare:  reghdfe  z_welfare  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority  , cl(vill_code tehs_code) abs(tribe_rel) 
estadd local controls = "Yes" 
eststo Welfare_2:  reghdfe  z_service_govt_imp  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority  , cl(vill_code tehs_code) abs(tribe_rel) 
estadd local controls = "Yes"  
eststo Security:  reghdfe  z_enforcement1  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority  , cl(vill_code tehs_code) abs(tribe_rel)
estadd local controls = "Yes"  
eststo Welf_Vs_Sec_1:  reghdfe  pca2  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority   , cl(vill_code tehs_code) abs(tribe_rel) 
estadd local controls = "Yes"  
eststo Welf_Vs_Sec_2:  reghdfe  pca2  z_tribal_frac0 z_Cseg z_inter man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority   , cl(vill_code tehs_code) abs(tribe_rel) 
estadd local controls = "Yes"  
esttab Welfare Welfare_2 Security Welf_Vs_Sec_1 Welf_Vs_Sec_2 using ${PathRes}table3_new.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) drop(_cons) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes





***************************************
****   			MECHANISM 		   ****
***************************************


*Tab 3
eststo clear
eststo Inter_Friend:  reghdfe z_friends_or z_tribal_frac0 z_Cseg z_inter man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs    ,   cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
eststo Non_Coop:  reghdfe z_informal_justice z_tribal_frac0 z_Cseg z_inter  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs    ,   cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
eststo Business_Serv:  reghdfe  z_bs_comm z_tribal_frac0 z_Cseg z_inter  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
eststo Trust_Other:  reghdfe z_trust_neigh z_tribal_frac0 z_Cseg z_inter  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs    ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
eststo Fear_Exploit:  reghdfe misuse z_tribal_frac0 z_Cseg z_inter man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs    ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
eststo Peace_Org:  reghdfe z_particip_peace z_tribal_frac0 z_Cseg z_inter  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs    ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
esttab Inter_Friend Non_Coop Business_Serv Trust_Other Fear_Exploit Peace_Org using ${PathRes}table4_new.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) stats(controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("ribal Fractionalization, Segregation, and Mechanisms") replace nonotes 



***************************************
****   			DISTANCE 		   ****
***************************************


bys tehs_code: egen mean_enemy_distance = mean(enemy_distance)
gen enemy_distance_imp = enemy_distance
replace enemy_distance_imp = mean_enemy_distance if enemy_distance==.
zval enemy_distance_imp
label variable z_enemy_distance_imp "Distance to outgroups"

*Tab 4
eststo clear
eststo Welfare:  reghdfe  z_welfare z_tribal_frac0 c.z_Cseg##c.z_enemy_distance_imp   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state service_state minority_vill minority_tehs  , cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
eststo Welfare_2:  reghdfe  z_service_govt_imp z_tribal_frac0 c.z_Cseg##c.z_enemy_distance_imp   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state service_state minority_vill minority_tehs  , cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
eststo Security:  reghdfe  z_enforcement1 z_tribal_frac0 c.z_Cseg##c.z_enemy_distance_imp   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state service_state minority_vill minority_tehs  , cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
eststo Welf_Vs_Sec_1:  reghdfe  pca2 z_tribal_frac0 c.z_Cseg##c.z_enemy_distance_imp   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state service_state minority_vill minority_tehs  , cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
esttab Welfare Welfare_2 Security Welf_Vs_Sec_1 using ${PathRes}table4_inter.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) stats(controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Fractionalization, Segregation, and Distance to Outgroups") replace nonotes 




******************************
****  BASIC CORRELATIONS  ****
******************************


* Tab A2
eststo clear
eststo Welf_Vs_Sec_1:  reghdfe pca2  z_tribal_frac0 z_Cseg    ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
estadd local sample = "Full sample"
eststo Welf_Vs_Sec_2:  reghdfe pca2  z_tribal_frac0 z_Cseg if vill_pop>=10  ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
estadd local sample = "Vill.>=N15."
eststo Welf_Vs_Sec_3:  reghdfe pca2  z_tribal_frac0 z_Cseg  if vill_pop>=15  ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
estadd local sample = "Vill. >= N15"
eststo Welf_Vs_Sec_4:  reghdfe pca2  z_tribal_frac0 z_Cseg  if vill_pop>=20 ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
estadd local sample = "Vill.>=N20"
esttab Welf_Vs_Sec_1 Welf_Vs_Sec_2 Welf_Vs_Sec_3 Welf_Vs_Sec_4 using ${PathRes}table1_new.rtf, b(3) se(3) drop(_cons) star(* 0.1 ** 0.05 *** 0.01) stats( controls sample r2 N, labels("Tribe FE" "Sample" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State") replace nonotes




******************************
****  		PCA1          ****
******************************

* Tab A3
eststo clear
eststo Welf_Vs_Sec_1:  reghdfe pca1  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs     ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
estadd local sample = "Full sample"
eststo Welf_Vs_Sec_2:  reghdfe pca1  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs  if vill_pop>=10  ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
estadd local sample = "Vill.>=N15."
eststo Welf_Vs_Sec_3:  reghdfe pca1  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   if vill_pop>=15  ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
estadd local sample = "Vill. >= N15"
eststo Welf_Vs_Sec_4:  reghdfe pca1  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   if vill_pop>=20 ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
estadd local sample = "Vill.>=N20"
esttab Welf_Vs_Sec_1 Welf_Vs_Sec_2 Welf_Vs_Sec_3 Welf_Vs_Sec_4 using ${PathRes}table1_pca.rtf, b(3) se(3) drop(_cons) star(* 0.1 ** 0.05 *** 0.01) stats( controls sample r2 N, labels("Tribe FE" "Sample" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Welfare and Security-Oriented State (PCA1)") replace nonotes





******************************
****     SEPARATELY       ****
******************************

* Tab A4
eststo clear
eststo Welfare_1:  reghdfe  z_service_govt_imp  z_tribal_frac0    man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs  , noabsorb cl(vill_code tehs_code)
eststo Welfare_2:  reghdfe  z_service_govt_imp   z_Cseg   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs  , noabsorb cl(vill_code tehs_code)  
eststo Welf_Vs_Sec_1:  reghdfe  pca2   z_tribal_frac0     man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs  , noabsorb cl(vill_code tehs_code)
eststo Welf_Vs_Sec_2:  reghdfe  pca2   z_Cseg   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs  , noabsorb cl(vill_code tehs_code)
esttab  Welfare_1 Welfare_2 Welf_Vs_Sec_1 Welf_Vs_Sec_2 using ${PathRes}table3_sep.rtf, b(3) se(3)  star(* 0.1 ** 0.05 *** 0.01) drop(_cons) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes




******************************
****     SERVICES         ****
******************************

* Tab A5
eststo clear
eststo Services_state_1:  reghdfe z_service_state z_tribal_frac0 z_Cseg man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice never_married minority_vill minority_tehs  , noabsorb cl(vill_code tehs_code)
estadd local controls = "Yes"
eststo Services_state_2:  reghdfe z_service_state z_tribal_frac0 z_Cseg man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice never_married minority_vill minority_tehs frac_village_minority frac_tehsil_minority , noabsorb cl(vill_code tehs_code)
estadd local controls = "Yes"
eststo Security_state_1:  reghdfe z_security_state z_tribal_frac0 z_Cseg man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice never_married minority_vill minority_tehs  , noabsorb cl(vill_code tehs_code)
estadd local controls = "Yes"
eststo Security_state_2:  reghdfe z_security_state z_tribal_frac0 z_Cseg man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice never_married minority_vill minority_tehs frac_village_minority frac_tehsil_minority , noabsorb cl(vill_code tehs_code)
estadd local controls = "Yes"
esttab Services_state_1 Services_state_2 Security_state_1 Security_state_2 using ${PathRes}table_services_new.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) drop(_cons) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Service Provision by the State (controls)") replace nonotes




******************************
****    CONTACT COND.     ****
******************************


pca re_imp ms_imp edu_imp infra_imp wat_imp sani_imp as_imp bs_imp bank_imp rs_imp 
predict serv_imp
label variable tribe_adv "Tribe status"

* Tab A6
eststo clear
eststo Welf_Vs_Sec_2:  reghdfe pca2 z_tribal_frac0 z_Cseg z_inter man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs tribe_adv serv_imp   ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
eststo Welf_Vs_Sec_4:  reghdfe  pca2  z_tribal_frac0 z_Cseg z_inter man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs tribe_adv serv_imp frac_village_minority frac_tehsil_minority    ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
esttab Welf_Vs_Sec_2  Welf_Vs_Sec_4 using ${PathRes}table4_cond.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) keep(z_tribal_frac0 z_Cseg z_inter tribe_adv minority_vill minority_tehs  frac_village_minority frac_tehsil_minority serv_imp) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes


******************************
****    COMPOSITIONAL     ****
******************************


bys tribe_rel tehs_code: egen tribe_status = mean(tribe_discr)
bys tribe_rel tehs_code: egen tribe_income = mean(lnmont_incm)
gen countT=1
bys tribe_rel tehs_code: egen tribe_size_temp = sum(countT)
gen tribe_size = tribe_size_temp/2798
gen lntribe_size = ln(tribe_size+1)

label variable tribe_income "Tribe income"
label variable tribe_status "Tribe discrim."
label variable tribe_size "Tribe size"
label variable lntribe_size "Tribe size (log)"

* Tab A7
eststo clear
eststo Welf_Vs_Sec_2:  reghdfe pca2 z_tribal_frac0 z_Cseg z_inter man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state never_married minority_vill minority_tehs tribe_income tribe_status lntribe_size   ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
eststo Welf_Vs_Sec_4:  reghdfe  pca2  z_tribal_frac0 z_Cseg z_inter man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state never_married minority_vill minority_tehs  frac_village_minority frac_tehsil_minority  tribe_income tribe_status lntribe_size  ,  cl(vill_code tehs_code) noabsorb
estadd local controls = "Yes"
esttab Welf_Vs_Sec_2  Welf_Vs_Sec_4 using ${PathRes}table5_new.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) keep(z_tribal_frac0 z_Cseg z_inter  tribe_income tribe_status lntribe_size minority_vill minority_tehs  frac_village_minority frac_tehsil_minority) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes


******************************
****    AGENCY/FR FEs     ****
******************************

* Tab A8
eststo clear
eststo Welf_Vs_Sec_2:  reghdfe pca2 z_tribal_frac0 z_Cseg z_inter man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state never_married minority_vill minority_tehs    , abs(tribe_rel agency_code) cl(tehs_code vill_code)
estadd local controls = "Yes"
eststo Welf_Vs_Sec_4:  reghdfe  pca2  z_tribal_frac0 z_Cseg z_inter man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state never_married minority_vill minority_tehs  frac_village_minority frac_tehsil_minority    , abs(tribe_rel agency_code) cl(tehs_code vill_code)
estadd local controls = "Yes"
esttab Welf_Vs_Sec_2  Welf_Vs_Sec_4 using ${PathRes}table6_new.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) keep(z_tribal_frac0 z_Cseg z_inter minority_vill minority_tehs  frac_village_minority frac_tehsil_minority) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes



******************************
****      CATEGORIES      ****
******************************

* Fig A6
cibar pca2, over1(village_type)




******************************
****    EXTREME BOUNDS    ****
******************************


* impute extreme values 

gen taxes1_lb = taxes1
replace taxes1_lb = 0 if idp==1

gen taxes1_ub = taxes1
replace taxes1_ub = 4 if idp==1

gen enforcement1_lb = enforcement1
replace enforcement1_lb = 0 if idp==1

gen enforcement1_ub = enforcement1
replace enforcement1_ub = 4 if idp==1


pca taxes1_ub enforcement1_ub
predict pca1_ub pca2_ub

pca taxes1_lb enforcement1_lb
predict pca1_lb pca2_lb


* Fig A5
	eststo clear
	eststo Naive:  reghdfe pca2  z_tribal_frac0 z_Cseg     , abs(tribe_rel) cl(vill_code tehs_code)
	estadd local controls = "Yes"
	eststo Controls:  reghdfe pca2 z_tribal_frac0  z_Cseg   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority   , abs(tribe_rel) cl(vill_code tehs_code)
	estadd local controls = "Yes"
	eststo Controls_UpperBound:  reghdfe pca2_ub z_tribal_frac0  z_Cseg   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority   , abs(tribe_rel) cl(vill_code tehs_code)
	estadd local controls = "Yes"
	eststo Controls_LowerBound:  reghdfe pca2_lb z_tribal_frac0  z_Cseg   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority    , abs(tribe_rel) cl(vill_code tehs_code)
	estadd local controls = "Yes"
	coefplot Naive Controls Controls_UpperBound Controls_LowerBound, keep( z_tribal_frac0 z_Cseg) xline(0) horizontal byopts(xrescale) level(95 90) plotregion(lcolor(black)) xlabel(,grid nogextend ) ylabel(,grid nogextend ) aspectratio(1.0) title("Welfare Vs Security")






***************************************
****   		DIFF SEGREGAT 	       ****
***************************************

* Tab A9
eststo clear
eststo Welfare:  reghdfe  z_welfare  z_tribal_frac0 z_tribal_frac1  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority  , cl(vill_code tehs_code) noabsorb 
eststo Welfare_2:  reghdfe  z_service_govt_imp  z_tribal_frac0 z_tribal_frac1  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority  , cl(vill_code tehs_code) noabsorb 
eststo Security:  reghdfe  z_enforcement1  z_tribal_frac0 z_tribal_frac1  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority  , cl(vill_code tehs_code) noabsorb
eststo Welf_Vs_Sec_1:  reghdfe  pca2  z_tribal_frac0 z_tribal_frac1  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority   , cl(vill_code tehs_code) noabsorb 
eststo Welf_Vs_Sec_2:  reghdfe  pca2  z_tribal_frac0 z_tribal_frac1 z_inter man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs frac_village_minority frac_tehsil_minority   , cl(vill_code tehs_code) noabsorb 
esttab Welfare Welfare_2 Security Welf_Vs_Sec_1 Welf_Vs_Sec_2 using ${PathRes}table3_diffseg.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) drop(_cons) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes




***************************************
****   		DISPLACEMENT 	       ****
***************************************

bys vill_code: egen mean_idp_vill = mean(idp)
bys tehs_code: egen mean_idp_tehs = mean(idp)

zval mean_idp_vill mean_idp_tehs

* Tab A10
eststo clear
eststo Welfare:  reghdfe  z_welfare  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs  z_mean_idp_vill z_mean_idp_tehs , cl(vill_code tehs_code) noabsorb 
eststo Welfare_2:  reghdfe  z_service_govt_imp  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs  z_mean_idp_vill z_mean_idp_tehs , cl(vill_code tehs_code) noabsorb
eststo Security:  reghdfe  z_enforcement1  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs  z_mean_idp_vill z_mean_idp_tehs , cl(vill_code tehs_code) noabsorb
eststo Welf_Vs_Sec_1:  reghdfe  pca2  z_tribal_frac0 z_Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   z_mean_idp_vill z_mean_idp_tehs , cl(vill_code tehs_code) noabsorb 
eststo Inter:  reghdfe  pca2  c.z_tribal_frac0##c.z_mean_idp_vill c.z_Cseg##c.z_mean_idp_tehs  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb 
esttab Welfare Welfare_2 Security Welf_Vs_Sec_1 Inter using ${PathRes}table3_disp.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) drop(_cons) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes



***************************************
****   		NEW OUTCOME 	       ****
***************************************

* Tab A11
eststo clear
eststo Inter1:  reghdfe  z_service_govt_imp z_welfare   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice    , cl(vill_code )  noabsorb 
eststo Inter2:  reghdfe  z_service_govt_imp z_enforcement1   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice    , cl(vill_code )  noabsorb 
eststo Inter3:  reghdfe  z_service_govt_imp pca2   man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice    , cl(vill_code )  noabsorb 
esttab Inter1 Inter2 Inter3 using ${PathRes}table3_newoutc.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) keep(z_welfare z_enforcement1 pca2) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes



***************************************
****   		CHRONIC ILL 	       ****
***************************************

* Tab A12
eststo clear
reghdfe  chronicly_ill victimization  man age never_married educ1 educ2 shia  lnpop  lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs  , cl(vill_code) noabsorb 
esttab  using ${PathRes}table3_ill.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) keep(victimization) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes



***************************************
****   		NOMADIC TRIBE 	       ****
***************************************



gen ahmadzai_ghilzai_powindah = 0
replace ahmadzai_ghilzai_powindah = 1 if strpos(lower(tribe), "wazir") > 0
replace ahmadzai_ghilzai_powindah = 1 if strpos(lower(tribe), "mehsud") > 0
replace ahmadzai_ghilzai_powindah = 1 if strpos(lower(tribe), "dawar") > 0

label variable ahmadzai_ghilzai_powindah "Nomadic"

* Tab A14
eststo clear
eststo Welfare:  reghdfe  z_welfare  c.tribal_frac0##i.ahmadzai_ghilzai_powindah c.Cseg##ahmadzai_ghilzai_powindah  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb 
eststo Welfare_2:  reghdfe  z_service_govt_imp  c.tribal_frac0##i.ahmadzai_ghilzai_powindah c.Cseg##ahmadzai_ghilzai_powindah  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb 
eststo Security:  reghdfe  z_enforcement1  c.tribal_frac0##i.ahmadzai_ghilzai_powindah c.Cseg##ahmadzai_ghilzai_powindah  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb
eststo Welf_Vs_Sec_1:  reghdfe  pca2  c.tribal_frac0##i.ahmadzai_ghilzai_powindah c.Cseg##ahmadzai_ghilzai_powindah  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs    , cl(vill_code tehs_code) noabsorb 
esttab Welfare Welfare_2 Security Welf_Vs_Sec_1 using ${PathRes}table3_nomadic.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) drop(_cons) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes




***************************************
****   		INTRATRIBAL 	       ****
***************************************


label variable intratribefractionalization "Intra-tribal division"

* Tab A15
eststo clear
eststo Welfare:  reghdfe  z_welfare  c.tribal_frac0##c.intratribefractionalization c.Cseg##c.intratribefractionalization  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb 
eststo Welfare_2:  reghdfe  z_service_govt_imp  c.tribal_frac0##c.intratribefractionalization c.Cseg##c.intratribefractionalization  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb 
eststo Security:  reghdfe  z_enforcement1  c.tribal_frac0##c.intratribefractionalization c.Cseg##c.intratribefractionalization  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb
eststo Welf_Vs_Sec_1:  reghdfe  pca2  c.tribal_frac0##c.intratribefractionalization c.Cseg##c.intratribefractionalization  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs    , cl(vill_code tehs_code) noabsorb 
esttab Welfare Welfare_2 Security Welf_Vs_Sec_1 using ${PathRes}table3_intratribe.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) drop(_cons man age never_married educ1 educ2 shia  lnpop victimization chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs ) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes




***************************************
****   		IDENTIFICATION 	       ****
***************************************


recode local_id 1=5 2=4 5=3 3=2 4=1, gen(local_id_rec)
label variable local_id_rec "Tribal identification"

* Tab A16
eststo clear
eststo Welfare:  reghdfe  z_welfare  c.tribal_frac0##c.local_id_rec c.Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb 
eststo Welfare_2:  reghdfe  z_service_govt_imp  c.tribal_frac0##c.local_id_rec c.Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb 
eststo Security:  reghdfe  z_enforcement1  c.tribal_frac0##c.local_id_rec c.Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb
eststo Welf_Vs_Sec_1:  reghdfe  pca2  c.tribal_frac0##c.local_id_rec c.Cseg  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs    , cl(vill_code tehs_code) noabsorb 
esttab Welfare Welfare_2 Security Welf_Vs_Sec_1 using ${PathRes}table3_identif.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) drop(_cons man age never_married educ1 educ2 shia  lnpop victimization chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs ) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes




***************************************
****   		OTHER DIVERSE 	       ****
***************************************


label variable education_ihi_01 "Education HHI"
label variable age_ihi_01 "Age HHI"
label variable wealth_ihi_01 "Wealth HHI"

* Tab A17
eststo clear
eststo Welfare:  reghdfe  z_welfare  education_ihi_01 age_ihi_01 wealth_ihi_01  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb 
eststo Welfare_2:  reghdfe  z_service_govt_imp  education_ihi_01 age_ihi_01 wealth_ihi_01  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb 
eststo Security:  reghdfe  z_enforcement1  education_ihi_01 age_ihi_01 wealth_ihi_01  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs   , cl(vill_code tehs_code) noabsorb
eststo Welf_Vs_Sec_1:  reghdfe  pca2  education_ihi_01 age_ihi_01 wealth_ihi_01  man age never_married educ1 educ2 shia  lnpop c.victimization##c.chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs    , cl(vill_code tehs_code) noabsorb 
esttab Welfare Welfare_2 Security Welf_Vs_Sec_1 using ${PathRes}table3_otherdiv.rtf, b(3) se(3) star(* 0.1 ** 0.05 *** 0.01) drop(_cons man age never_married educ1 educ2 shia  lnpop victimization chronicly_ill lnmont_incm ngo_justice service_state security_state minority_vill minority_tehs ) stats( controls r2 N, labels("Controls" "R-squared" "N")) label mtitles title("Tribal Fractionalization, Segregation, and Support for Comprehensive Welfare State vs Security-Oriented State (controls)") replace nonotes






